package com.sharding5.multi.mybatisplus.seata.algorithm;

import com.google.common.collect.Range;
import com.google.common.collect.Sets;
import org.apache.shardingsphere.sharding.api.sharding.standard.*;

import java.util.*;

/**
 * @author lenovo
 */
public class MyOrderTableShardingAlgorithm implements StandardShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        Long curValue = preciseShardingValue.getValue();
        String curTable = "";
        if (curValue > 0 && curValue <= 100) {
            curTable = "t_order_1";
        } else if (curValue > 100 && curValue <= 200) {
            curTable = "t_order_2";
        } else if (curValue > 200 && curValue <= 300) {
            curTable = "t_order_3";
        } else {
            curTable = "t_order_4";
        }
        return curTable;
    }

    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {
        Range<Long> valueRange = rangeShardingValue.getValueRange();
        Long lowerEndpoint = valueRange.lowerEndpoint();
        Long upperEndpoint = valueRange.upperEndpoint();

        Set<String> curTable = Sets.newHashSet();
        if (lowerEndpoint > 0 && lowerEndpoint <= 100) {
            if (upperEndpoint > 0 && upperEndpoint <= 100) {
                curTable.add("t_order_1");
            } else if (upperEndpoint > 100 && upperEndpoint <= 200) {
                curTable.add("t_order_1");
                curTable.add("t_order_2");
            } else if (upperEndpoint > 200 && upperEndpoint <= 300) {
                curTable.add("t_order_1");
                curTable.add("t_order_2");
                curTable.add("t_order_3");
            } else if (upperEndpoint > 300) {
                curTable.add("t_order_1");
                curTable.add("t_order_2");
                curTable.add("t_order_3");
                curTable.add("t_order_4");
            }
        } else if (lowerEndpoint > 100 && lowerEndpoint <= 200) {
            if (upperEndpoint > 100 && upperEndpoint <= 200) {
                curTable.add("t_order_2");
            } else if (upperEndpoint > 200 && upperEndpoint <= 300) {
                curTable.add("t_order_2");
                curTable.add("t_order_3");
            } else if (upperEndpoint > 300) {
                curTable.add("t_order_2");
                curTable.add("t_order_3");
                curTable.add("t_order_4");
            }
        } else if (lowerEndpoint > 200 && lowerEndpoint <= 300) {
            if (upperEndpoint > 200 && upperEndpoint <= 300) {
                curTable.add("t_order_3");
            } else if (upperEndpoint > 300) {
                curTable.add("t_order_3");
                curTable.add("t_order_4");
            }
        } else if (lowerEndpoint > 300) {
            curTable.add("t_order_4");
        }
        return curTable;
    }

    @Override
    public void init(Properties props) {
    }

    @Override
    public String getType() {
        return "MY_TABLE_SHARDING_ALGORITHM_FOR_ORDER";
    }
}